package cn.edu.zzuli.nothinglink.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Data
@Configuration
public class ThreadPoolConfig {

    @Value("${nothing-link.thread.coreSize}")
    private Integer coreSize;
    @Value("${nothing-link.thread.maxSize}")
    private Integer maxSize;
    @Value("${nothing-link.thread.keepAliveTime}")
    private Integer keepAliveTime;

    /**
     * 线程池
     * @return
     */
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(){
        ThreadPoolExecutor executor = new ThreadPoolExecutor(coreSize,
                maxSize,
                keepAliveTime,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue(1000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());

        return executor;
    }
}
